package com.wgtgt.file_extension;

import java.io.File;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;

public class Application {

	public static void scan(String src, String dest) {
		File srcFile = new File(src);
		File destFile = new File(dest);
		String absolutePathSrc = srcFile.getAbsolutePath();
		String absolutePathDest = destFile.getAbsolutePath();
		if (!FileUtil.exist(absolutePathDest)) {
			FileUtil.mkdir(absolutePathDest);
		}
		scan(srcFile, destFile, absolutePathSrc, absolutePathDest);
	}

	private static void scan(File srcFile, File destFile, String absolutePathSrc, String absolutePathDest) {
		if (srcFile.isFile()) {
			String suffix = FileNameUtil.getSuffix(srcFile);
			File destFileNew = new File(destFile, suffix);
			if (!destFileNew.exists()) {
				destFileNew.mkdirs();
			}
			String name = srcFile.getName();
			File destFileComplete = new File(destFileNew, name);
			if (destFileComplete.exists()) {
				long lastModified = srcFile.lastModified();
				String prefixDest = FileNameUtil.getPrefix(name);
				String suffixDest = FileNameUtil.getSuffix(name);
				String simpleName = prefixDest + "-" + lastModified + "." + suffixDest;
				File destFileCompleteNew = new File(destFileNew, simpleName);
				String destPathCompleteNew = destFileCompleteNew.getAbsolutePath();
				FileUtil.rename(srcFile, destPathCompleteNew, true);
			} else {
				String destPathComplete = destFileComplete.getAbsolutePath();
				FileUtil.rename(srcFile, destPathComplete, true);
			}
		} else {
			File[] listFiles = srcFile.listFiles();
			if (listFiles != null) {
				for (int i = 0; i < listFiles.length; i++) {
					File fl = listFiles[i];
					String srcFileNew = fl.getAbsolutePath();
					scan(fl, destFile, srcFileNew, absolutePathDest);
				}
			}
		}
	}

}
